function get(config = {}) {
	var that = this;
	var formData = config.formData || {};
	var url = getApp().globalData.http + config.url;
	uni.request({
		url: url+ '?' + tansParams(formData),
		method: 'GET',
		header: {
			'Authorization': uni.getStorageSync('token'),
		},
		dataType: 'json',
		success: function(resData) {
			if (resData.statusCode == 200) {
				if (resData.data.code == 200) {
					config.success(resData.data);
				}else if(resData.data.code == 401){
					uni.showToast({
						title: resData.data.code + '：' + resData.data.msg,
						icon: 'none',
						complete: () => {
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								});
							}, 1500); // 延迟1秒后跳转
						}
					});
				} 
				else {
					uni.showToast({
						title: resData.data.code + '：' + resData.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			} else {
				console.log(resData);
				uni.showToast({
					title: resData.statusCode + '：' + resData.data.error,
					icon: 'none',
					duration: 2000
				})
			}
		},
		error: function(e) {
			that.msg("网络异常，请稍后再试");
		}
	})
};

function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}

function post(config = {}) {
	var that = this;
	var formData = config.formData || {};
	var url = getApp().globalData.http + config.url;
	uni.request({
		url: url,
		data: formData,
		method: 'POST',
		header: {
			'content-type': 'application/json',
			'Authorization': uni.getStorageSync('token'),
		},
		dataType: 'json',
		success: function(resData) {
			if (resData.statusCode == 200) {
				if (resData.data.code == 200) {
					config.success(resData.data);
				}else if(resData.data.code == 401){
					uni.showToast({
						title: resData.data.code + '：' + resData.data.message,
						icon: 'none',
						complete: () => {
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								});
							}, 1500); // 延迟1秒后跳转
						}
					});
				} 
				else {
					uni.showToast({
						title: resData.data.code  + '：' + resData.data.message,
						icon: 'none',
						duration: 2000
					})
				}
			} else {
				console.log(resData);
				uni.showToast({
					title: resData.statusCode + '：' + resData.data.error,
					icon: 'none',
					duration: 2000
				})
			}
		},
		error: function(e) {
			that.msg("网络异常，请稍后再试");
		}
	})
};



export default {
	get,
	post
}